using System;
using System.Data;
using System.Linq;
namespace Epm.AppLogic.Framework
{
	/// <summary>
	/// Summary description for CUtil.
	/// </summary>
	public class CUtil
    { 
        public static DataTable CloneDt(DataTable fromTable)
        { 
            return fromTable.Clone();
        }

        public static DataSet CloneDt(DataSet fromDataSet)
        {
            DataSet dstem332 = fromDataSet.Clone(); 
            return dstem332 ;
        }

		public static void CopyDataTable(DataTable fromTable,DataTable toTable)
		{
			CopyDataTable(fromTable,toTable,true);
		}
		public static void CopyDataTable(DataTable fromTable,DataTable toTable,bool accept)
		{
			toTable.Clear();
			foreach (DataRow drw in fromTable.Rows)
			{
                DataRow drwTo = toTable.NewRow();
                CopyDataRow(drw, drwTo); 
				toTable.Rows.Add(drwTo);
			}
			if (accept) toTable.AcceptChanges();
		}
		public static void CopyDataSet(DataSet fromDataSet,DataSet toDataSet)
		{
			foreach (DataTable fromTable in fromDataSet.Tables)
			{
				CopyDataTable(fromTable,toDataSet.Tables[fromTable.TableName]);
			}
		}
		public static void CopyDataSet(DataSet fromDataSet,DataSet toDataSet,bool accept)
		{
			foreach (DataTable fromTable in fromDataSet.Tables)
			{
				CopyDataTable(fromTable,toDataSet.Tables[fromTable.TableName],accept);
			}
		}
		public static void CopyDataRow(DataRow fromRow,DataRow toRow)
        {
            CopyDataRow(fromRow, toRow,"",""); 
		}
        public static void CopyDataRow(DataRow fromRow, DataRow toRow,string strpNotCopyCols,string strpOnlyCopyCols)
        {
            if (strpNotCopyCols == string.Empty && strpOnlyCopyCols==string.Empty)
            {
                foreach (DataColumn dcl in fromRow.Table.Columns)
                {
                    if (toRow.Table.Columns.IndexOf(dcl.ColumnName) < 0) continue;
                    if (fromRow[dcl.ColumnName].ToString() == string.Empty && toRow.Table.Columns[dcl.ColumnName].DataType != typeof(string) && dcl.DataType == typeof(string))
                    {
                        toRow[dcl.ColumnName] = DBNull.Value;
                    }
                    else
                    {
                        toRow[dcl.ColumnName] = fromRow[dcl.ColumnName];
                    }
                }
            }
            else
            {
                string[] arrtrm111 = strpNotCopyCols.Split(',');
                string[] arrtrm222 = strpOnlyCopyCols.Split(',');
                foreach (DataColumn dcl in fromRow.Table.Columns)
                {
                    if (strpNotCopyCols != string.Empty && arrtrm111.Contains(dcl.ColumnName))
                    {
                        continue;
                    }
                    if (strpOnlyCopyCols != string.Empty && !arrtrm222.Contains(dcl.ColumnName))
                    {
                        continue;
                    }
                    if (toRow.Table.Columns.IndexOf(dcl.ColumnName) < 0) continue;
                    //toRow[dcl.ColumnName] = fromRow[dcl.ColumnName];
                    if (fromRow[dcl.ColumnName].ToString() == string.Empty && toRow.Table.Columns[dcl.ColumnName].DataType != typeof(string) && dcl.DataType == typeof(string))
                    {
                        toRow[dcl.ColumnName] = DBNull.Value;
                    }
                    else
                    {
                        toRow[dcl.ColumnName] = fromRow[dcl.ColumnName];
                    }
                }
            }
        }
		public static void CopyDataRow(DataRowView fromRow,DataRow toRow)
        {
            CopyDataRow(fromRow.Row, toRow); 
		}
		public static void CopyDataRow(DataRowView fromRow,DataRowView toRow)
        {
            CopyDataRow(fromRow.Row, toRow.Row); 
		}
		public static void CopyDataRow(DataRow fromRow,DataRowView toRow)
		{
            CopyDataRow(fromRow, toRow.Row); 
		}
         
	}
      
     
}
